package com.kuqi.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kuqi.MediaPlayerActivity;
import com.kuqi.R;
import com.kuqi.entitiy.VideoItem;

import java.util.List;

public class VideoGridAdapter extends BaseAdapter {

    // 父容器
    private Context mContext;
    // 视频信息列表
    private List<VideoItem> mList;


    // 构造
    public VideoGridAdapter(Context mContext, List<VideoItem> mList){
        this.mContext = mContext;
        this.mList = mList;
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public VideoItem getItem(int position) {
        return mList == null ? null : mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        VideoItem video_item = getItem(position);
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.homepage_item, null);
            mHolder = new ViewHolder();
            mHolder.recommend_image = convertView.findViewById(R.id.recommend_image);
            mHolder.recommend_title = convertView.findViewById(R.id.recommend_title);
            mHolder.recommend_sub_title = convertView.findViewById(R.id.recommend_sub_title);
            convertView.setTag(mHolder);
        }else{
            mHolder = (ViewHolder) convertView.getTag();
        }

        Glide.with(mContext).load(video_item.getThumbnail()).into(mHolder.recommend_image);
        mHolder.recommend_title.setText(video_item.getTheme());
        mHolder.recommend_sub_title.setText(video_item.getDescribe());

        // 为内容对象(视频item)添加点击事件
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // 获取点击的视频所在item位置的视频实例信息
                VideoItem videoinfo = mList.get(position);

                // 携带视频参数跳转
                Intent intent = new Intent(mContext, MediaPlayerActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("video_info", videoinfo);
                intent.putExtras(bundle);
                mContext.startActivity(intent);

                // 动画过渡
                ((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_no);
            }
        });

        return convertView;
    }


    // 用于临时缓存用于展现的数据
    private static class ViewHolder{
        private ImageView recommend_image;
        private TextView recommend_title;
        private TextView recommend_sub_title;
    }
}
